package com.app.filter;

import org.apache.log4j.Logger;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class SSOFilter implements Filter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SSOFilter.class);
	
	protected final transient Log log = LogFactory.getLog(getClass());
	String systemid=""; 
	String sysAccount="";
	String sysPassword="";
	String url="";
	String isSSO = "false";
	String projectName="smsprj";
	/*
	 * (non-Java-doc)
	 * 
	 * @see java.lang.Object#Object()
	 */
	public SSOFilter() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.Filter#init(FilterConfig arg0)
	 */
	public void init(FilterConfig config) throws ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("init(FilterConfig) - start");
		}

		systemid = config.getInitParameter("systemid");
		sysAccount = config.getInitParameter("sysAccount");
		sysPassword = config.getInitParameter("sysPassword");
		url = config.getInitParameter("url");
		isSSO = config.getInitParameter("isSSO");

		if (logger.isDebugEnabled()) {
			logger.debug("init(FilterConfig) - end");
		}
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.Filter#doFilter(ServletRequest arg0, ServletResponse
	 *      arg1, FilterChain arg2)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		if (logger.isDebugEnabled()) {
			HttpServletRequest rq=(HttpServletRequest)request;
			logger
					.debug("c通过过滤器，请求目录为："+rq.getServletPath());
		}
		
		
		HttpServletRequest rq=(HttpServletRequest)request;
		HttpServletResponse rsp=(HttpServletResponse)response;
		HttpSession session = rq.getSession(false);
		String username=null;
		//rq.getRequestDispatcher("/relogin.jsp").forward(rq, rsp);
		//System.out.println("SSOFilter is doing.....");
		//System.out.println("session="+session);
		if(null==session && !rq.getRequestURI().endsWith("Login.jsp")){
			if (logger.isDebugEnabled()) {
				logger
						.debug("用户没有登录系统，重新登录！");
			}
			
			rq.getRequestDispatcher("/relogin.jsp").forward(rq, rsp);
		}
		if("true".equals(isSSO))
		{
			Cookie cookies[] = rq.getCookies();  //�ۺ�Ӧ��ƽ̨��COOKIE
			String cookieid = null;
			String cid = null;
			if(cookies != null)
			for(int i=0;i<cookies.length; i++) 
			{}
			if(null==username)
			{//b.���û�гɹ���¼�� ��ת��ͳһ��¼����
				try {
					log.info("username is null,we login from http://tiap.sg.gmcc.net/login.aspx");
					rsp.sendRedirect("http://tiap.sg.gmcc.net/login.aspx");

					if (logger.isDebugEnabled()) {
						logger
								.debug("doFilter(ServletRequest, ServletResponse, FilterChain) - end");
					}
					return;
				} catch (Exception e) {
					logger.error(
							"doFilter(ServletRequest, ServletResponse, FilterChain)", e);

					e.printStackTrace();
				}
				
			}
			else
			{
				
				
			}//
				
			

			
		}
		//System.out.println("SSOFilter is done.....");
		
		try {
			chain.doFilter(request, response);
		} catch (RuntimeException e) {
			HttpServletRequest rq1=(HttpServletRequest)request;
			logger
					.error("请求出错目录为："+rq1.getServletPath());
			logger.error(e);
//			e.printStackTrace();
		}

		if (logger.isDebugEnabled()) {
			logger
					.debug("doFilter(ServletRequest, ServletResponse, FilterChain) - end");
		}
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

}